const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    index2: 0,
    index3: 0,
    dishi: ['----请选择招考地市----'],
    nsh: ['----请选择招考农商行----'],
    phone: 0,
    theList: ''
  },


  dishiChange: function (e) {
    let num = e.detail.value;
    let that = this;
    console.log('dishi发送选择改变，携带值为', that.data.dishi[num]);
    this.setData({
      index: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=44456&callback=?', //接收url
      data: {
        level: '2',
        grfiled: 'dishi',
        grtext: that.data.dishi[num]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        var list = [];
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].nsh)
        }
        that.setData({
          nsh: list
        });
      }
    })
  },
  nshChange: function (e) {
    var that = this;
    console.log('nsh发送选择改变，携带值为', that.data.nsh[e.detail.value])
    this.setData({
      index2: e.detail.value
    })
  },

  onLoad: function (options) {
    var that = this;
    console.log(options);
    that.setData({
      area: options.area,
      geneal: options.agent
    })
    if (options.page) {
      that.setData({
        page: options.page
      })
    }
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=44456&callback=?', //接收url
      data: {
        level: '1',
        dishi: ''
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        list.push('----请选择报考地市----')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].dishi)
        }
        that.setData({
          dishi: list
        });
      }
    })


  },


  getPhoneNumber(e) {
    var that = this;
    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      var that = this;
      that.setData({
        phone: res.result,
      })
      wx.setStorageSync('mianshiphone', res.result)
      console.log(res.result)
      wx.request({
        url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
        method: 'POST',
        data: {
          activeId: 23,
          mobile: res.result,
          areaCode: that.data.area,
          agent: that.data.geneal,
          pageId: that.data.page
        },
        success: res => {
          console.log(res);
        }
      })
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/chaxun/register?actid=44456&callback=?', //接收url
        data: {
          dishi: that.data.dishi[that.data.index],
          nsh: that.data.nsh[that.data.index2],
          phone: res.result,
          fenxiao: that.data.area,
          geneal: that.data.geneal,
        }, //接收参数
        success(res) {
          console.log(res)
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
          console.log(theJson)

          
          wx.request({ //发送请求
            url: "https://zg99.offcn.com/index/chaxun/getlist/?actid=44456&callback=?",
            data: {
              dishi: that.data.dishi[that.data.index],
              nsh: that.data.nsh[that.data.index2],
              limits: '100'
            }, //接收参数
            success(res) { //返回
              var jsonStr = res.data;
              jsonStr = jsonStr.replace("(", "");
              jsonStr = jsonStr.replace(")", "");
              if (typeof jsonStr != 'object') {
                jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                var theJson = JSON.parse(jsonStr);
              };
              console.log(theJson)
              if (theJson.status === 1) {
                that.setData({
                  theList: theJson.lists
                })
                wx.pageScrollTo({
                  scrollTop:1500,
                  duration: 1500,
                })
                console.log(that.data.theList)
              } else {
                wx.showToast({
                  title: '暂无数据！',
                })
              }

            }
          })
        }
      })
    }).catch(err => {
      console.error(err);
    });
  },
  tijiao: function () {
    var that = this;
    var dishi = that.data.dishi[that.data.index];
    var nsh = that.data.nsh[that.data.index2];
    wx.request({ //发送请求
      url: "https://zg99.offcn.com/index/chaxun/getlist/?actid=44456&callback=?",
      data: {
        dishi: dishi,
        nsh: nsh,
        limits: '100'
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        };
        console.log(theJson)
        if (theJson.status === 1) {
          that.setData({
            theList: theJson.lists
          })
          wx.pageScrollTo({
            scrollTop:1500,
            duration: 1500,
          })
          console.log(that.data.theList)
        } else {
          wx.showToast({
            title: '暂无数据！',
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onshow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onshareAppMessage: function () {
    var that = this;
    return {
      title: '2022山东农商行面试考情查询',
      path: 'pages/mianshi/mianshi?area=' + that.data.area + '&agent=' + that.data.geneal + '&page=' + that.data.page
    }
  },
  /**分享到朋友圈*/
  onshareTimeline: function () {
    var that = this;
    return {
      title: '2022山东农商行面试考情查询',
      path: 'pages/mianshi/mianshi?area=' + that.data.area + '&agent=' + that.data.geneal + '&page=' + that.data.page
    }
  }
})